<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var reorganizeString = function (s) {
      const cntMap = new Map(), n = s.length
      for (let i = 0; i < s.length; i++) cntMap.set(s[i], (cntMap.get(s[i]) || 0) + 1)
      const arr = Array.from(cntMap).sort((a, b) => b[1] - a[1])
      if (arr[0][1] > (n + 1) / 2) return ''
      const resArr = new Array(arr[0][1]).fill(arr[0][0]), len = resArr.length
      let idx = 0
      for (let i = 1; i < arr.length; i++) {
        for (let j = 0; j < arr[i][1]; j++) {
          resArr[idx] += arr[i][0]
          idx = (idx + 1) % len
        }
      }
      return resArr.join('')
    };
    console.log(reorganizeString("aab"))
  </script>
</body>

</html>